;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; OGRP-OS Commands - Draw Graphical User Interface					;
;										;
; This package is free software; you can redistribute it and/or modify		;
; it under the terms of the GNU General Public License as published by		;
; the Free Software Foundation; either version 2 of the License, or		;
; (at your option) any later version.						;
;										;
; This package is distributed in the hope that it will be useful,		;
; but WITHOUT ANY WARRANTY; without even the implied warranty of		;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the			;
; GNU General Public License for more details.					;
;										;
; You should have received a copy of the GNU General Public License		;
; along with this package; if not, write to the Free Software			;
; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA	;
;										;
; Version 0.1									;
; Copyright 2009 JockePockee Mr3D						;
; Email: jockpockee@gmail.com, alson@passagen.se				;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Change to graphic mode 12h (640x480, 16 colours)
push	cs
pop	ds
mov	ah, 0
mov	al, 12h
int	10h

;--------------------

mov	al, 1
call	clear_screen
call	draw_screen
mov	al, 2
call	clear_screen
call	draw_screen

jmp	$

mov	ah, 0
mov	al, 3
int	10h



retf

clear_screen:	

	mov	dx, 480d

	clear:
		mov	cx, 640d

		clear_row:
			dec	cx
			dec	dx
			call	draw_pixel
			inc	dx
			inc	cx
			loop	clear_row

		dec	dx

		cmp	dx, 0
		jne	clear

	clear_end:
		ret

draw_pixel:
;CX, DX - X, Y
;AL - colour (0d to 15d)
push	ax
; push	bx
push	cx
push	dx
; push	es	;Slowdown


mov	[Colour], al
mov	ax, [Line]
mul	dx
push	ax
mov	ax, cx
div	byte [Eight]
mov	[BitOffset], ah		;Modulus from divide gives bit offset
xor	ah, ah
mov	cx, ax
pop	bx

add	bx, cx			;BX here contains the memory positon of the byte, we just need to add the offset bits.

; mov	ax, 0A000h
; mov	es, ax			;Video Memmory Segment

mov	cx, 4
PlaneLoop:
	push	cx

	dec	cx
	mov	ax, 0960h
	mul	cx
	add	ax, 7A80h
	mov	es, ax
	
	mov	al, [Colour]
	
	mov	ch, 0000_0001b
	shr	al, cl	;shl ch, cl
	and	al, ch
	
	mov	ch, [BitOffset]
	mov	cl, 7
	sub	cl, ch
	
	mov	ah, 0000_0001b
	shl	ax, cl
	
	mov	ch, [es:bx]
	
	;Set bit or clear bit?
	cmp	al, 0
	je	BitClear
		or	ch, al	;Set
		jmp	BitSetEnd
	BitClear:
		not	ah
		and	ch, ah	;Clear
	BitSetEnd:
	
	mov	[es:bx], ch
	
	pop	cx
loop	PlaneLoop

; pop	es	;Slowdown
pop	dx
pop	cx
; pop	bx
pop	ax
ret

draw_screen:
	;Here the backbuffer will be copied to the framebuffer att 0A000h
	push	ds
	mov	ax, 0A000h
	mov	es, ax			;Video Memmory Segment
	
	mov	cx, 4
	PlaneCopy:
		push	cx
		
		dec	cx
		mov	ax, 0960h
		mul	cx
		add	ax, 7A80h
		mov	ds, ax
		
		mov	al, 2	
		mov	dx, 03c4h
		out	dx, al		;Output 2 to prepare the card for recieving info about the active plane(s)
		
		mov	al, 0000_0001b
		shl	al, cl
		mov	dx, 03c5h
		out	dx, al		;Here we send info about the active plane, each plane is a bit in low nibble
		
		xor	bx, bx
		CopyLoop:
			mov	al, [ds:bx]
			mov	[es:bx], al
			inc	bx
		cmp	bx, 38401d
		jne	CopyLoop
		
		pop	cx
	loop	PlaneCopy
	pop	ds
ret


Line		dw 80d ;(640/8)
Two		db 2
Eight		db 8
Colour		db 0
BitOffset	db 0